package network.themis.rms.console.service;

import lombok.extern.slf4j.Slf4j;
import network.themis.rms.console.common.model.PageModel;
import network.themis.rms.console.common.model.RestResp;
import network.themis.rms.console.entity.user.User;
import network.themis.rms.console.repo.user.UserRepo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ccl
 * @time 2018-07-16 16:12
 * @name UserService
 * @desc:
 */

@Slf4j
@Service
public class UserService {

    @Resource
    private UserRepo userRepo;

    public RestResp page(Integer pageSize, Integer pageNo){
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pager = PageRequest.of(pageNo-1, pageSize, sort);
        try{
            Page<User> page = userRepo.findAll(pager);
            PageModel result = new PageModel(page.getTotalPages(), page.getTotalElements(), page.getContent());
            return RestResp.success(result);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return RestResp.fail();
    }
}
